package org.soc.timesheet.domain;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;

@Entity
@Table(name = "ROLES")
@Configurable
@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "ROLES")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "functionss", "subFunctionss", "userss" })
@RooJson
@JsonIgnoreProperties({ "functionss", "subFunctionss", "userss" })
public class Roles {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("functionss", "subFunctionss", "userss").toString();
    }

    @PersistenceContext
    transient EntityManager entityManager;

    public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("");

    public static final EntityManager entityManager() {
        EntityManager em = new Roles().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countRoleses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Roles o", Long.class).getSingleResult();
    }

    public static List<Roles> findAllRoleses() {
        return entityManager().createQuery("SELECT o FROM Roles o", Roles.class).getResultList();
    }

    public static List<Roles> findAllRoleses(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Roles o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Roles.class).getResultList();
    }

    public static Roles findRoles(Integer id) {
        if (id == null) return null;
        return entityManager().find(Roles.class, id);
    }

    public static List<Roles> findRolesEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Roles o", Roles.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static List<Roles> findRolesEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Roles o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Roles.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Roles attached = Roles.findRoles(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public Roles merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Roles merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public String toJson(String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(this);
    }

    public static Roles fromJsonToRoles(String json) {
        return new JSONDeserializer<Roles>().use(null, Roles.class).deserialize(json);
    }

    public static String toJsonArray(Collection<Roles> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static String toJsonArray(Collection<Roles> collection, String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(collection);
    }

    public static Collection<Roles> fromJsonArrayToRoleses(String json) {
        return new JSONDeserializer<List<Roles>>().use("values", Roles.class).deserialize(json);
    }

    @ManyToMany(mappedBy = "roleses")
    private Set<Functions> functionss;

    @ManyToMany(mappedBy = "roleses")
    private Set<SubFunctions> subFunctionss;

    @OneToMany(mappedBy = "idRole")
    private Set<Users> userss;

    @Column(name = "COD_ROLE", length = 5)
    private String codRole;

    @Column(name = "DESC_ROLE", length = 45)
    private String descRole;

    public Set<Functions> getFunctionss() {
        return functionss;
    }

    public void setFunctionss(Set<Functions> functionss) {
        this.functionss = functionss;
    }

    public Set<SubFunctions> getSubFunctionss() {
        return subFunctionss;
    }

    public void setSubFunctionss(Set<SubFunctions> subFunctionss) {
        this.subFunctionss = subFunctionss;
    }

    public Set<Users> getUserss() {
        return userss;
    }

    public void setUserss(Set<Users> userss) {
        this.userss = userss;
    }

    public String getCodRole() {
        return codRole;
    }

    public void setCodRole(String codRole) {
        this.codRole = codRole;
    }

    public String getDescRole() {
        return descRole;
    }

    public void setDescRole(String descRole) {
        this.descRole = descRole;
    }
}
